#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: socket.sh
# Description: 
#   
# Edit History: 
#   2010-12-16    File created.
#========================================================================

#How to forcibly close a socket in TIME_WAIT?
#TIME-WAIT - represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request.
#/etc/init.d/networking restart

#顯示所有開啟的socket's address, port, pid, user
sockstat

lsof

#列程式開啟的address, port
netstat -p


#殺掉tcp的方法
# 1.tcpkill command
#   (a) Kill all outgoing ftp (port 21) connection:
#sudo tcpkill -i eth0 port 21

#   (b) Kill all all packets arriving at or departing from host 192.168.1.2 (host12.nixcraft.com)
#sudo tcpkill host 192.168.1.2

#    OR
#sudo tcpkill host host12.nixcraft.com

#   (c) To kill all IP packets between 192.168.1.2 and any host except 192.168.1.111, type the following:
#sudo tcpkill ip host 192.168.1.2 and not 192.168.1.111

# 2.cutter command

#tcpdrop (freebsd)

#顯示目前TCP連線狀態
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 

#關掉socket的方法
#You can close a socket in sh just like any other file: exec 42>&- where 42 is the file descriptor. 
#What you can't do (except in a few shells that provide an extension for it) is open a socket. But of course that closes the socket in the shell, not in another process.

#Closing a socket in a running process would disrupt its behavior in a way that the author of the program is not supposed to expect — it's like going in and modifying a piece of memory. Still, it can be done by connecting to the process with a debugger and making it execute a socket closing API call (that would be the close or shutdown system call on unix).
